<?php

/**
* MySQL 数据库操作类
*
* @author H.R.M <heizes@21cn.com>
*/

error_reporting(7);

class db_MySQL {
	/**
        * 服务器名或 ip 地址
        *
        * @var string
        */

	var $server = "localhost";

	/**
        * 数据库名
        *
        * @var string
        */

	var $database = "";

	/**
        * 用户名
        *
        * @var string
        */

	var $user = "root";

	/**
        * 用户密码
        *
        * @var string
        */

	var $password = "";

	/**
        * 是否使用持续连接
        *
        * @var bool
        */

	var $usepconnect = false;

	/**
        * 是否打开 debug 模式
        *
        * @var bool
        */

	var $debug = false;

	/**
        * MySQL link identifier
        *
        * @var resource
        */

	var $id;

	/**
        * SQL query 次数
        *
        * @var integer
        */

	var $querycount = 0;

	/**
        * 运行 SQL 请求后返回的结果集
        *
        * @var resource
        */

	var $result;

	/**
        *
        * @var array
        */

	var $record = array();

	var $rows;

	/**
        * 最后一次 INSERT 操作所返回的自增 ID
        *
        * @var integar
        */

	var $insertid;

	/**
        * 当出错时, 是否停止运行?
        *
        * @var bool 1: 停止, 2: 继续运行
        */

	var $halt = 1;

	/**
        * 错误号
        *
        * @var integer
        */

	var $errno;

	/**
        * 错误提示
        *
        * @var string
        */

	var $error;

	/**
        * SQL运行记录
        *
        * @var array
        */

	var $querylog = array();

	/**
        * 是否 cache 结果集, query_first, fetch_one_array 有效
        *
        * @var bool
        */

	var $use_cache = true;

	/**
        * 初始化
        */

	function db_MySQL() {
		// 初始化
		global $memc;

		$this->memc = $memc;
	}

	/**
        * 获取错误描述
        *
        * @access private
        * @return string
        */

	function geterrdesc() {
		$this->error = @mysql_error($this->id);

		return $this->error;
	}

	/**
        * 获取错误号
        *
        * @access private
        * @return integer
        */

	function geterrno() {
		$this->errno = @mysql_errno($this->id);

		return $this->errno;
	}

	/**
        * 连接数据库
        *
        * @access public
        * @return resource Returns a MySQL link identifier
        */

	function connect() {
		if ($this->usepconnect) {
			if (!$this->id = @mysql_pconnect($this->server, $this->user, $this->password)) {
				$this->halt("数据库链接失败");
			}
		} else {
			if (!$this->id = mysql_connect($this->server, $this->user, $this->password, true)) {
				$this->halt("数据库链接失败");
			}
		}

		$this->selectdb();

		return $this->id;
	}

	/**
        * 选择数据库
        *
        * @access public
        */

	function selectdb() {
		if (!mysql_select_db($this->database, $this->id)) {
			$this->halt("选择数据库失败");
		}
	}

	/**
        * 运行 SQL 语句并返回结果集
        *
        * @access public
        * @param  $query_string string
        * @return resource |false
        */

	function query($query_string, $cache = 1) {
		// $this->result = mysql_query($query_string, $this->id);
		$this->result = mysql_db_query($this->database, $query_string, $this->id);

		if (!$this->result) {
			$this->halt("SQL 无效: " . $query_string);
		}

		//$fp = fopen('/var/tmp/lixian_query_log.log', 'a+');
		//fwrite($fp, "Date: " . date("Y-m-d H:i:s") . "\r\n" . $query_string . "\r\n\r\n");
		//fclose($fp);

		$this->querycount++;

		return $this->result;
	}

	/**
        * Fetch a result row as an associative array, a numeric array, or both.
        *
        * @access public
        * @param  $result , $result_type
        * @see mysql_fetch_array
        * @return array |false
        */

	function fetch_array($result, $result_type = MYSQL_ASSOC) {
		if (!$result) {
			$this->halt("resource result 无效:" . $result);
		}

		$this->record = mysql_fetch_array($result, $result_type);

		return $this->record;
	}

	/**
        * Get a result row as an enumerated array
        *
        * @access public
        * @param  $result
        * @return array |false
        */

	function fetch_row($result) {
		if (!$result) {
			$this->halt("resource result 无效:" . $result);
		}

		$this->record = mysql_fetch_row($result);

		return $this->record;
	}

	/**
        * 运行 SQL 并返回结果
        *
        * @access public
        * @return array
        */

	function fetch_one_array($query_string, $result_type = MYSQL_ASSOC, $use_cache = 1) {
		// $this->result = $this->query($query);
		// $this->record = $this->fetch_array($this->result, $result_type);
		$this->record = $this->query_first($query_string, $result_type, $use_cache);

		return $this->record;
	}

	/**
        * 运行 SQL 并返回结果
        *
        * @access public
        * @param string $query_string
        * @param integer $result_type
        * @see mysql_fetch_array
        * @param bool $use_cache 是否使用 cache, 默认是使用
        * @return array |false
        */

	function query_first($query_string, $result_type = MYSQL_ASSOC, $use_cache = 1) {
		$set_cache = 0;

		if ($this->use_cache AND $use_cache) {
			$key = 'db_' . $query_string . "_" . $result_type;

			$cache = new SmartCache($key);

			$cache->lifetime = 60;

			$this->record = $cache->read($key);

			if ($this->record) {
				return $this->record;
			} else {
				$set_cache = 1;
			}
		}

		$this->result = $this->query($query_string);

		$this->record = $this->fetch_array($this->result, $result_type);

		if ($set_cache) {
			$cache->write($this->record);
		}

		return $this->record;
	}


	/**
        * 运行 SQL 并以数组的形式返回所有结果
        *
        * @access public
        * @param string $query_string
        * @param integer $result_type
        * @return array |false
        */

	function query_all($query_string, $result_type = MYSQL_ASSOC) {

		$this->result = $this->query($query_string);

		if($this->result){

			$records = array();
			while($record = $this->fetch_array($this->result, $result_type)){
				$records[] = $record;
			}


		}

		return $records;
	}


	/**
        * Get number of rows in result
        *
        *
        *
        * returns the number of rows in a result set. This command is only valid for SELECT statements.
        *
        * @access public
        * @param  $result
        * @return integer
        */

	function num_rows($result) {
		$this->rows = mysql_num_rows($result);

		return $this->rows;
	}

	/**
        * Free result memory
        *
        * @access public
        * @param  $result
        */

	function free_result($result) {
		if (!mysql_free_result($result)) {
			$this->halt("释放结果集失败");
		}
	}

	/**
        * Get the ID generated from the previous INSERT operation
        *
        * @access public
        * @return integer
        */

	function insert_id() {
		$this->insertid = mysql_insert_id($this->id);

		if (!$this->insertid) {
			$this->halt("fail to get mysql_insert_id");
		}

		return $this->insertid;
	}

	/**
        * Get number of affected rows in previous MySQL operation
        *
        * @return integer returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with link_identifier
        */

	function affected_rows() {
		$this->affected_rows = mysql_affected_rows($this->id);

		return $this->affected_rows;
	}

	function date_seek($result, $i) {
		if (mysql_date_seek($result, $i)) {
			return true;
		} else {
			return false;
		}
	}

	/**
        * 关闭数据库连接
        *
        * @access public
        */

	function close() {
		@mysql_close($this->id);
	}

	/**
        * 建过 array 建立条件
        */

	function build_condition($condition = array(), $bool = " AND ") {
		if ($condition AND is_array($condition)) {
			$conditions = " WHERE " . implode($bool, $condition);
		}

		return $conditions;
	}

	/**
        * 提示出错信息并中终程序
        *
        * @access private
        * @param  $msg 提示信息
        */

	function halt($msg) {

		//nocache_header();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		// always modified
		header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
		header("Cache-Control: post-check=0, pre-check=0", false);

		header("Pragma: no-cache"); // HTTP/1.0

		$log = '';

		$log .= str_replace("\n", "\r\n", $msg) . "\r\n";
		$log .= "______________________________________________________________________\r\n";
		$log .= "Date: " . date("Y-m-d H:i:s") . "\r\n";
		$log .= "mysql error description: " . str_replace("\n", "\r\n", $this->geterrdesc() ). "\r\n";
		$log .= "mysql error number: " . $this->geterrno() . "\r\n";
		$log .= "Database: " . $this->database . "\r\n";
		$log .= "Server: " . $this->server . "\r\n";
		$log .= "Linkid " . $this->id . "\r\n";
		$log .= "Script: " . getenv("REQUEST_URI") . "\r\n";
		$log .= "Referer: " . getenv("HTTP_REFERER") . "\r\n\r\n\r\n";

		@error_log($log, 3, 'E:/projects/php/svn-php/xiaozu.xunlei.com/db_error.log');
		// exit("系统繁忙，请稍后再试！");
		if ($this->halt) {
			// if ($_SESSION['v_isadmin'] OR $_GET['debugKey'] == 'movfox717') {
			//echo("<pre>$log</pre>");
			// }
			echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>服务器忙,请稍后再访问!</body></html>";
			exit;
		}
	}
}

?>